<?php

use think\migration\Seeder;
use think\Db;

class BatMatchSeeder extends Seeder
{

    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * http://docs.phinx.org/en/latest/seeding.html
     */
    public function run()
    {
        for ($i = 0; $i <= 50; $i++) {
            $address = new \app\admin\model\BatMatchModel();
            $bat_id = Db::name('bat_match')->insertGetId($this->faker());
            Db::name('give')->insert($this->giveFaker($bat_id));
            Db::name('size')->insert($this->sizeFaker($bat_id));
            Db::name('victory')->insert($this->victoryFaker($bat_id));

        }

    }

    //生产随机小数
    function randFloat($min = 0, $max = 1)
    {
        $rand = $min + mt_rand() / mt_getrandmax() * ($max - $min);
        return floatval(number_format($rand, 2));
    }

    public function giveFaker($id)
    {
        return [
            'bat_id'      => $id,
            'g_main_odd'  => $this->randFloat(),
            'g_give_name' => '平球',
            'g_give_num'  => mt_rand(1, 5),
            'g_pass_odd'  => $this->randFloat(),
            'create_time' => time(),
            'update_time' => time(),
        ];

    }

    public function sizeFaker($id)
    {
        return [
            'bat_id'     => $id,
            's_main_odd' => $this->randFloat(),
            's_size_num' => mt_rand(1, 5),
            's_pass_odd' => $this->randFloat(),
            'create_time' => time(),
            'update_time' => time(),
        ];

    }

    public function victoryFaker($id)
    {
        return [
            'bat_id'       => $id,
            'v_main_odd'   => $this->randFloat(),
            'v_middle_odd' => $this->randFloat(),
            'v_pass_odd'   => $this->randFloat(),
            'create_time' => time(),
            'update_time' => time(),
        ];

    }


    public function faker()
    {
        $faker = Faker\Factory::create('zh_CN');
        $lea_name = ['NBA', 'CNA', '英超', '中超', '德甲', '意甲'];
        $status = mt_rand(0, 3);
        $main_score = null;
        $pass_score = null;
        if ($status == 3) {
            $main_score = mt_rand(1, 2);
            $pass_score = mt_rand(1, 2);
        }

        $type = mt_rand(0, 2);
        $teamId = \app\admin\model\TeamModel::where('type', $type)->column('id');

        return [
            'lea_name'     => $faker->randomElement($lea_name),
            'type'         => $type,
            'status'       => $status,
            'team_main_id' => $faker->randomElement($teamId),
            'team_pass_id' => $faker->randomElement($teamId),
            'main_score'   => $main_score,
            'pass_score'   => $pass_score,
            'match_time'   => time(),
            'bet_end_time' => time() + mt_rand(100, 1000),
            'fake_num'     => mt_rand(1, 500),
            'create_time' => time(),
            'update_time' => time(),

        ];
    }
}